import datetime
import json

from django.core import serializers
from django.forms import model_to_dict
from django.http import HttpResponse, JsonResponse
from django.shortcuts import render

# Create your views here.
from baseinfo.helpfun import query_to_list
from baseinfo.models import Liscense_name, Liscense_level, Liscense_organ, Company, Role, Outage, Team, Feedback_type, \
    Fail_type, Result_type, Event_character, Department
from etdata.models import Signal, Tank


def hello(request):
    return HttpResponse('hello')


def liscensebaseinfo(request):
    if request.method == 'GET':
        liscense_name = Liscense_name.objects.all()
        liscense_level = Liscense_level.objects.all()
        liscense_organ = Liscense_organ.objects.all()
        res = {
            'data': {
                'liscense_name': query_to_list(liscense_name),
                'liscense_level': query_to_list(liscense_level),
                'liscense_organ': query_to_list(liscense_organ),
                     },
            'meta': {'status': 200, 'message': '获取证书基本信息成功'}
        }
        return JsonResponse(res)


def memberinfo(request):
    if request.method == 'GET':
        company = Company.objects.all()
        role = Role.objects.all()
        res = {
            'data': {
                'company': query_to_list(company),
                'role': query_to_list(role)
            },
            'meta': {'status': 200, 'message': '获取成员基本信息成功'}
        }
        return JsonResponse(res)


def filter_info(request):
    if request.method == 'GET':
        liscense_name = Liscense_name.objects.all()
        liscense_level = Liscense_level.objects.all()
        liscense_organ = Liscense_organ.objects.all()
        company = Company.objects.all()
        role = Role.objects.all()
        res = {
            'data': {
                'liscense_name': query_to_list(liscense_name),
                'liscense_level': query_to_list(liscense_level),
                'liscense_organ': query_to_list(liscense_organ),
                'company': query_to_list(company),
                'role': query_to_list(role)
                     },
            'meta': {'status': 200, 'message': '获取证书基本信息成功'}
        }
        return JsonResponse(res)


def feedback_info(request):
    if request.method == 'GET':
        outages = Outage.objects.all().order_by('-start_date')
        teams = Team.objects.all()
        feedback_types = Feedback_type.objects.all()
        fail_types = Fail_type.objects.all()
        result_types = Result_type.objects.all()
        event_characters = Event_character.objects.all()
        res = {
            'data': {
                'outages': query_to_list(outages),
                'teams': query_to_list(teams),
                'feedback_types': query_to_list(feedback_types),
                'fail_types': query_to_list(fail_types),
                'result_types': query_to_list(result_types),
                'event_characters': query_to_list(event_characters)
            },
            'meta': {'status': 200, 'message': '经验反馈的选项基本信息获取成功'}
        }
        return JsonResponse(res)


def defect_info(request):
    if request.method == 'GET':
        outages = Outage.objects.all().order_by("-start_date")
        departments = Department.objects.all()
        teams = Team.objects.all()
        res = {
            'data': {'outages': query_to_list(outages), 'departments': query_to_list(departments), 'teams': query_to_list(teams)},
            'meta': {'status': 200, 'message': '获取新增缺陷的选项信息成功！'}
        }
        return JsonResponse(res)


def etdata_info(request):
    if request.method == 'GET':
        outages = Outage.objects.all().order_by("-start_date")
        tanks_list = []
        tanks = Tank.objects.values('name').distinct()
        for tank in tanks:
            tanks_list.append(tank['name'])
        signals = Signal.objects.all().distinct()
        signals_list = []
        for signal in signals:
            signals_list.append(signal.name)


        res = {
            'data': {'outages': query_to_list(outages), 'signals': signals_list, 'tanks': tanks_list},
            'meta': {'status': 200, 'message': '获取涡流基本信息成功！'}
        }
        return JsonResponse(res)

# 获取大修轮次列表
def get_outage(request):
    if request.method == 'GET':
        query = request.GET.get('query')
        if query:
            outages = Outage.objects.filter(outage__contains=query.upper()).order_by('-start_date')
        else:
            outages = Outage.objects.all().order_by('-start_date')
        if outages.count() >= 50:
            outages = outages[0:50]
        res = {
            'data': {'outages': query_to_list(outages)},
            'meta': {'status': 200, 'message': '获取大修轮次数据成功！'}
        }
        return JsonResponse(res)

# 根据id值删除大修轮次
def delete_outage(request, outage_id):
    if request.method == 'DELETE':
        outage = Outage.objects.filter(pk=outage_id)
        if outage.count():
            outage[0].delete()
            res = {
                'data': {},
                'meta': {'status': 200, 'message': "大修轮次删除成功！"}
            }
        else:
            res = {
                'data': {},
                'meta': {"status": 201, 'message': "未找到要删除的大修轮次信息"}
            }
        return JsonResponse(res)


def add_outage(request):
    if request.method == 'POST':
        data = json.loads(request.body)
        outage = data.get('outage').upper()
        if(Outage.objects.filter(outage=outage).count()):
            res = {
                'data': {},
                'meta': {'status': 201, 'message': '需要添加的大修轮次已经存在'}
            }
        else:
            new_outage = Outage()
            new_outage.outage = outage
            new_outage.start_date = datetime.datetime.strptime(data.get('startDate')[0:10], '%Y-%m-%d')
            new_outage.end_date = datetime.datetime.strptime(data.get('endDate')[0:10], '%Y-%m-%d')
            new_outage.save()
            res = {
                'data': {},
                'meta': {'status': 200, 'message': "添加大修轮次成功"}
            }

        return JsonResponse(res)

# 编辑大修轮次
def edit_outage(request):
    if request.method == 'PUT':
        data = json.loads(request.body)
        outage = data.get('outage')
        # print(data)
        outage_query = Outage.objects.filter(outage=outage)
        if outage_query.count():
            outage_obj = outage_query.first()
            outage_obj.start_date = datetime.datetime.strptime(data.get('startDate')[0:10], '%Y-%m-%d')
            outage_obj.end_date = datetime.datetime.strptime(data.get('endDate')[0:10], '%Y-%m-%d')
            outage_obj.save()
            res = {
                'data': {},
                'meta': {'status': 200, 'message': '修改大修轮次信息成功！'}
            }
        else:
            res = {
                'data': {},
                'meta': {'status': 201, 'message': '未找到要修改的大修轮次信息'}
            }
        return JsonResponse(res)